<template>
  <div ref="el" :class="className" :style="{ height, width }"></div>
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref, nextTick } from 'vue'
import * as echarts from 'echarts'
import { debounce } from 'lodash-es'

// 引入 macarons 主题
import 'echarts/theme/macarons'

// 定义组件名称
defineOptions({
  name: 'RaddarChart',
})

// 定义组件属性
interface Props {
  className?: string
  width?: string
  height?: string
}

const props = withDefaults(defineProps<Props>(), {
  className: 'chart',
  width: '100%',
  height: '300px',
})

// 图表实例
const chart = ref<echarts.ECharts | null>(null)
const animationDuration = 3000

// 获取 DOM 元素的引用
const el = ref<HTMLDivElement | null>(null)

// Resize 相关逻辑
let sidebarElm: HTMLElement | null = null
let resizeHandler: (() => void) | null = null

const $_sidebarResizeHandler = (e: TransitionEvent) => {
  if (e.propertyName === 'width' && resizeHandler) {
    resizeHandler()
  }
}

const initListener = () => {
  resizeHandler = debounce(() => {
    resize()
  }, 100)
  window.addEventListener('resize', resizeHandler)

  sidebarElm = document.getElementsByClassName('sidebar-container')[0] as HTMLElement
  if (sidebarElm)
    sidebarElm.addEventListener('transitionend', $_sidebarResizeHandler as EventListener)
}

const destroyListener = () => {
  if (resizeHandler) {
    window.removeEventListener('resize', resizeHandler)
    resizeHandler = null
  }

  if (sidebarElm) {
    sidebarElm.removeEventListener('transitionend', $_sidebarResizeHandler as EventListener)
    sidebarElm = null
  }
}

const resize = () => {
  if (chart.value) {
    chart.value.resize()
  }
}

// 初始化图表
const initChart = () => {
  if (el.value) {
    chart.value = echarts.init(el.value, 'macarons')

    chart.value.setOption({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          // 坐标轴指示器，坐标轴触发有效
          type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
        },
      },
      radar: {
        radius: '66%',
        center: ['50%', '42%'],
        splitNumber: 8,
        splitArea: {
          areaStyle: {
            color: 'rgba(127,95,132,.3)',
            opacity: 1,
            shadowBlur: 45,
            shadowColor: 'rgba(0,0,0,.5)',
            shadowOffsetX: 0,
            shadowOffsetY: 15,
          },
        },
        indicator: [
          { name: 'Sales', max: 10000 },
          { name: 'Administration', max: 20000 },
          { name: 'Information Techology', max: 20000 },
          { name: 'Customer Support', max: 20000 },
          { name: 'Development', max: 20000 },
          { name: 'Marketing', max: 20000 },
        ],
      },
      legend: {
        left: 'center',
        bottom: '10',
        data: ['Allocated Budget', 'Expected Spending', 'Actual Spending'],
      },
      series: [
        {
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1,
            },
          },
          data: [
            {
              value: [5000, 7000, 12000, 11000, 15000, 14000],
              name: 'Allocated Budget',
            },
            {
              value: [4000, 9000, 15000, 15000, 13000, 11000],
              name: 'Expected Spending',
            },
            {
              value: [5500, 11000, 12000, 15000, 12000, 12000],
              name: 'Actual Spending',
            },
          ],
          animationDuration: animationDuration,
        },
      ],
    })
  }
}

// 生命周期钩子
onMounted(() => {
  nextTick(() => {
    initChart()
    initListener()
  })
})

onBeforeUnmount(() => {
  if (!chart.value) {
    return
  }
  chart.value.dispose()
  chart.value = null
  destroyListener()
})
</script>
